const express=require("express");
const router=express.Router()
const pool = require('../index.js'); // 引入数据库连接池

// 新增路由以从数据库获取数据
router.get('/get-counts', (req, res) => {
    pool.query('SELECT device_name, count FROM device_counts', (error, results) => {
      if (error) throw error;
      // 将查询结果转换为前端所需的格式
      const chartData = results.map(item => ({
        title: item.device_name,
        num: item.count
      }));
      res.send({ chartData });
    });
  });
  router.put('/update-device-count/:id', (req, res) => {
    const { id } = req.params;
    const { count } = req.body;
    pool.query('UPDATE device_counts SET count = ? WHERE id = ?', [count, id], (err, result) => {
      if (err) {
        return res.status(500).json({ message: 'Database error', error: err.message });
      }
      if (result.affectedRows === 0) {
        return res.status(404).json({ message: 'Device count not found' });
      }
      res.json({ message: 'Device count updated successfully' });
    });
  });
module.exports=router;